from selenium.webdriver.support.select import Select
from selenium.webdriver.support.wait import WebDriverWait
from utils.logger import log
from utils.timer import sleep
from selenium.webdriver.common.action_chains import ActionChains

class Base:
    def __init__(self,driver):
        self.driver=driver

    #新增js方法
    def excute_js(self,script,*args):
        #执行JavaScript代码
        return self.driver.execute_script(script,*args)

    #滚动到元素
    def js_scroll(self,loc):
        self.excute_js("arguments[0].scrollIntoView();",self.base_find(loc))

    #js点击元素
    def js_click(self,loc):
        self.excute_js("arguments[0].click();",self.base_find(loc))

    #js设置元素值
    def js_set_value(self,loc,value):
        self.excute_js("arguments[0].value = arguments[1];", self.base_find(loc), value)

    #获取元素文本
    def js_get_text(self,loc):
        return self.excute_js("return arguments[0].textContent;",self.base_find(loc))

    #高亮元素
    def js_highlight(self,loc):
        el=self.base_find(loc)
        orig=el.get_attribute("style")
        self.excute_js("arguments[0].setAttribute('style', 'border:2px solid red;');", el)
        sleep()
        self.excute_js(f"arguments[0].setAttribute('style', '{orig}');", el)

    #移除元素属性
    def js_remove_attr(self,loc,attr):
        self.excute_js(f"arguments[0].removeAttribute('{attr}');",self.base_find(loc))


    def base_find(self,loc,timeout=30,poll=0.5):
        #查找元素
        # timeout:超时的时长一般30s，超时未找到报错
        #poll:检测时间隔时长，默认0.5s
        return WebDriverWait(self.driver,timeout,poll).until(lambda x: x.find_element(*loc))

    def base_input(self,loc,text):
        #输入文本
        el = self.base_find(loc)
        el.clear()
        if text is not None:
            el.send_keys(text)
            log.info(f"{el}输入文本:{text}")

    def base_click(self,loc):
        #点击
        self.base_find(loc).click()
        sleep()
        log.info(f'点击按钮：{loc}')

    def base_get_text(self,loc):
        #获取当前元素文本
        _text = self.base_find(loc).text
        log.info(f"获取文本：{_text}")
        return _text

    def base_get_title(self):
        #获取当前页title
        title = self.driver.title
        log.ingo(f"当前页面：{title}")
        return title

    def base_alert_confirm(self):
        #自动确认弹框 以便继续进行后续的测试操作
        self.driver.switchTo().alert.accept()

    def base_is_dis(self,loc):
        #查看元素是否可见
        state = self.base_find(loc).is_displayed()
        log.info(f"获取元素可见状态：{state}")
        return state

    def base_keep_press(self,loc,time):
        #保持长按
        ActionChains(self.driver).click_and_hold(self.base_find(loc)).perform()
        log.info(f"长按：{loc}")
        sleep(time)
        ActionChains(self.driver).release(self.base_find(loc)).perform()
        log.info(f"释放：{loc}")

    def base_select(self,loc,text):
        #下拉框选择
        # :param loc: select标签元素，父类，不是option
        # :param text:通过显示文本选择
        Select(self.base_find(loc)).select_by_visible_text(text)
        log.info(f"选择下拉框{log}的{text}")

    def base_tick_checkbox(self,loc,num):
        #勾选框
        checkbox_list = self.base_find(loc)
        action = ActionChains(self.driver)
        for i in range(0,num):
            action.move_to_element(checkbox_list[i])
            action.click(checkbox_list[i]).perform()
            sleep()

    def base_invisible_element(self,loc,num,stop):
        #对动态变化元素执行动作链
        msg = self.base_find(loc)
        for i in range(0,num):
            action = ActionChains(self.driver)
            action.move_to_element(msg[i])
            action.click(msg[i])
            action.perform()
            sleep(stop)

    def base_refresh(self):
        #刷新页面
        self.driver.refresh()

    def base_quit(self):
        #退出浏览器
        self.driver.quit()

